﻿package br.gov.bcb.leilao.web.componentes.behavior;

import java.io.Serializable;

import org.apache.wicket.Component;
import org.apache.wicket.Component.IVisitor;
import org.apache.wicket.MarkupContainer;
import org.apache.wicket.behavior.AbstractBehavior;
import org.apache.wicket.markup.html.form.AbstractTextComponent;
import org.apache.wicket.markup.html.form.CheckGroup;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.ListMultipleChoice;
import org.apache.wicket.markup.html.form.RadioGroup;
import org.apache.wicket.model.Model;

public class TransformarParaLabelTodosOsComponentesFilhos extends AbstractBehavior {

    private ToLabelVisitor labelVisitor = new ToLabelVisitor();

    @Override
    public void onConfigure(Component component) {
        if (component instanceof MarkupContainer) {
            MarkupContainer markupContainer = (MarkupContainer) component;

            configureLabels(markupContainer);
        }
    }

    private void configureLabels(MarkupContainer markupContainer) {
        markupContainer.visitChildren(DropDownChoice.class, labelVisitor);
        markupContainer.visitChildren(ListMultipleChoice.class, labelVisitor);
        markupContainer.visitChildren(AbstractTextComponent.class, labelVisitor);
        markupContainer.visitChildren(CheckGroup.class, labelVisitor);
        markupContainer.visitChildren(RadioGroup.class, labelVisitor);
    }

    private static class ToLabelVisitor implements IVisitor<Component>, Serializable {

        @Override
        public Object component(Component component) {
            component.setDefaultModel(new Model<Serializable>((Serializable) component.getDefaultModelObject()));
            component.add(new TransformarParaLabel());
            return null;
        }
    }
}
